package com.jsu.clubmanagementsystem.controller.application;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsu.clubmanagementsystem.common.BaseResponse;
import com.jsu.clubmanagementsystem.common.ErrorCode;
import com.jsu.clubmanagementsystem.common.PageQueryRequest;
import com.jsu.clubmanagementsystem.common.ResultUtils;
import com.jsu.clubmanagementsystem.constant.UserConstant;
import com.jsu.clubmanagementsystem.exception.BusinessException;
import com.jsu.clubmanagementsystem.model.dto.activityapplication.ActivityApplicationAddRequest;
import com.jsu.clubmanagementsystem.model.dto.activityapplication.ActivityApplicationQueryRequest;
import com.jsu.clubmanagementsystem.model.entity.Activity;
import com.jsu.clubmanagementsystem.model.entity.ActivityApplication;
import com.jsu.clubmanagementsystem.model.entity.User;
import com.jsu.clubmanagementsystem.model.enums.ApplicationTypeEnum;
import com.jsu.clubmanagementsystem.model.vo.activityApplication.ActivityApplicationVO;
import com.jsu.clubmanagementsystem.service.activity.ActivityService;
import com.jsu.clubmanagementsystem.service.application.ActivityApplicationService;
import com.jsu.clubmanagementsystem.service.user.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/activity-application")
public class ActivityApplicationController {
    @Resource
    private ActivityApplicationService activityApplicationService;

    @Resource
    private UserService userService;

    @Resource
    private ActivityService activityService;

    /**
     * 提交活动申请
     *
     * @param addRequest 申请请求
     * @param request    HTTP请求
     * @return 申请ID
     */
    @PostMapping("/add")
    public BaseResponse<Long> addApplication(@RequestBody ActivityApplicationAddRequest addRequest, HttpServletRequest request) {
        if (addRequest == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "参数为空");
        }
        return ResultUtils.success(activityApplicationService.processActivityApplication(addRequest));
    }

    /**
     * 查找活动申请 (分页)
     *
     * @param queryRequest 查询请求
     * @param request      HTTP请求
     * @return 申请列表
     */
    @PostMapping("/list")
    public BaseResponse<Page<ActivityApplicationVO>> listApplicationVoByPage(@RequestBody ActivityApplicationQueryRequest queryRequest, HttpServletRequest request) {
        if (queryRequest == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "参数为空");
        }

        User loginUser = userService.getLoginUser(request);
        if (loginUser.getUserRole().equals(UserConstant.USER_ROLE_USER)){
            queryRequest.setUserId(loginUser.getId());
        }
        Page<ActivityApplicationVO> page = activityApplicationService.listActivityApplicationVOByPage(queryRequest, loginUser);
        return ResultUtils.success(page);
    }

    /**
     * 查找活动申请
     *
     * @param id      申请ID
     * @param request HTTP请求
     * @return 申请详情
     */
    @GetMapping("/get")
    public BaseResponse<ActivityApplicationVO> getApplicationVoById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "无效的申请ID");
        }

        User loginUser = userService.getLoginUser(request);
        if (!loginUser.getUserRole().equals(UserConstant.USER_ROLE_ADMIN) &&
                !loginUser.getUserRole().equals(UserConstant.USER_ROLE_MANAGER)) {
            throw new BusinessException(ErrorCode.NO_AUTH, "无权限执行此操作");
        }

        ActivityApplication activityApplication = activityApplicationService.getById(id);
        if (activityApplication == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND, "申请不存在");
        }
        ActivityApplicationVO activityApplicationVO = activityApplicationService.getActivityApplicationVO(activityApplication);

        if (loginUser.getUserRole().equals(UserConstant.USER_ROLE_ADMIN)) {
            return ResultUtils.success(activityApplicationVO);
        }

        Integer type = activityApplication.getType();
        Long clubId = loginUser.getManagedClubId();
        if (type.equals(ApplicationTypeEnum.CREATE_ACTIVITY.getCode()) &&
                !activityApplication.getTargetId().equals(clubId)) {
            throw new BusinessException(ErrorCode.NO_AUTH, "非本社团的申请");
        } else {
            List<Activity> activityListOfClub = activityService.getActivityListOfClub(clubId);
            List<Long> activityIdListOfClub = activityListOfClub.stream()
                    .map(Activity::getId).toList();
            if (!activityIdListOfClub.contains(activityApplication.getTargetId())) {
                throw new BusinessException(ErrorCode.NO_AUTH, "非本社团的申请");
            }
        }
        return ResultUtils.success(activityApplicationVO);
    }

    @PostMapping("/getAll")
    public BaseResponse<Page<ActivityApplication>> getAllActivityApplication(@RequestBody PageQueryRequest page, HttpServletRequest request) {
        if (page == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "参数为空");
        }
        Page<ActivityApplication> page1 = activityApplicationService.getActivityApplicationVoPage(page);
        return ResultUtils.success(page1);
    }

    /**
     * 删除活动申请(及批量删除)
     */
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteActivityApplication(@RequestParam List<Long> ids){
        return ResultUtils.success(activityApplicationService.deleteByIds(ids));
    }
}
